import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/bean/bean_work.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/utils/date_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/18.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class MineWorkRecoderPage extends StatefulWidget {
  final UserBean userBean;

  const MineWorkRecoderPage({Key key, @required this.userBean})
      : super(key: key);

  @override
  _MineWorkRecoderPageState createState() => _MineWorkRecoderPageState();
}

class _MineWorkRecoderPageState extends State<MineWorkRecoderPage> {
  List<BeanWork> _list = [];

  @override
  void initState() {
    super.initState();
    _getUserWorkList();
  }

  bool _isLoading = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("成长记录"),
      ),
      backgroundColor: Colors.white,

      ///填充布局
      body: Container(
        margin: EdgeInsets.only(top: 40),
        child: buildListView(),
      ),
    );
  }

  Widget buildListView() {
    if (_isLoading) {
      return LoadingWidget();
    }
    if (_list == null || _list.length == 0) {
      return NoDataWidget(
        text: "暂无数据",
        clickCallBack: () {
          _getUserWorkList();
        },
      );
    }
    return ListView.builder(
      //Item数量
      itemCount: _list.length,
      itemBuilder: (BuildContext context, int index) {
        return buildListItemWidget(index);
      },
    );
  }

  Widget buildListItemWidget(int index) {
    BeanWork workBean = _list[index];
    return Container(
      height: 100,
      child: Row(
        children: [
          //第一列的日期
          buildLeftDate(workBean),
          //第二列的竖线
          buildMiddleLien(index),
          //第三列的职位信息
          buildWorkWidget(index),
        ],
      ),
    );
  }

  Widget buildLeftDate(BeanWork workBean) {
    return Container(
      width: 60,
      margin: EdgeInsets.only(left: 80, right: 10),
      child: Column(
        children: [
          Text(
            "${ViDateUtils.formatDateStr(workBean.createTime, format: "yyyy")}",
            style: TextStyle(
              fontWeight: FontWeight.w500,
              fontSize: 16,
              color: Color(0xff666666),
            ),
          ),
          Text(
            "${ViDateUtils.formatDateStr(workBean.createTime, format: "MM-dd")}",
            style: TextStyle(
              fontSize: 14,
              color: Color(0xff666666),
            ),
          ),
        ],
      ),
    );
  }

  Widget buildMiddleLien(int index) {
    Color color = Colors.green;
    if (index != 0) {
      color = Colors.grey[200];
    }
    return Column(
      children: [
        SizedBox(
          height: 4,
        ),
        ClipOval(
          child: Container(
            width: 6,
            height: 6,
            color: color,
          ),
        ),
        Expanded(
          child: Container(
            width: 1,
            color: index == _list.length - 1
                ? Colors.transparent
                : Colors.grey[300],
          ),
        )
      ],
    );
  }

  buildWorkWidget(int index) {
    BeanWork workBean = _list[index];
    return Container(
      margin: EdgeInsets.only(left: 12),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "${workBean.userPort}",
            style: TextStyle(
              fontWeight: FontWeight.w500,
              fontSize: 16,
              color: Color(0xff333333),
            ),
          ),
          Text(
            "${workBean.reason}",
            style: TextStyle(
              fontSize: 14,
              color: Color(0xff666666),
            ),
          ),
        ],
      ),
    );
  }

  Future<bool> _getUserWorkList() async {
    ResponseInfo partResponseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.userWorkList + "/${widget.userBean.userId}",
    );
    if (partResponseInfo.success) {
      List list = partResponseInfo.data;
      list.forEach((element) {
        BeanWork workBean = BeanWork.fromMap(element);
        _list.add(workBean);
      });
    } else {
      LogUtil.e("加载失败成功");
      ToastUtils.showToast("加载失败${partResponseInfo.message}");
    }

    _isLoading = false;
    if (mounted) {
      setState(() {});
    }

    return Future.value(true);
  }
}
